package com.meituan.catering.management.product.biz.model.converter;

import com.google.common.collect.Lists;
import com.meituan.catering.management.product.dao.model.ProductAccessoryDO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Believe
 * Date 2022/6/7 9:52
 */
public class ProductAccessoryFilter {
    public static List<ProductAccessoryDO> toUpdateList(List<ProductAccessoryDO> productAccessoryDOList, List<ProductAccessoryDO> queryAccessoryList) {
        List<ProductAccessoryDO> updateList = Lists.newLinkedList();
        Map<Long, List<ProductAccessoryDO>> longListMap = new HashMap<>();
        for (ProductAccessoryDO productAccessoryDO : queryAccessoryList) {
            if (longListMap.containsKey(productAccessoryDO.getId())) {
                List<ProductAccessoryDO> accessoryDOS = longListMap.get(productAccessoryDO.getId());
                accessoryDOS.add(productAccessoryDO);
            } else {
                List<ProductAccessoryDO> accessoryDOS = Lists.newLinkedList();
                accessoryDOS.add(productAccessoryDO);
                longListMap.put(productAccessoryDO.getId(), accessoryDOS);
            }
        }
        for (ProductAccessoryDO productAccessoryDO : productAccessoryDOList) {
            if (longListMap.containsKey(productAccessoryDO.getId())) {
                updateList.add(productAccessoryDO);
            }
        }
        return updateList;
    }

    public static List<ProductAccessoryDO> toInsertList(List<ProductAccessoryDO> productAccessoryDOList, List<ProductAccessoryDO> queryAccessoryList) {
        List<ProductAccessoryDO> insertList = Lists.newLinkedList();
        Map<Long, List<ProductAccessoryDO>> longListMap = new HashMap<>();
        for (ProductAccessoryDO productAccessoryDO : queryAccessoryList) {
            if (longListMap.containsKey(productAccessoryDO.getId())) {
                List<ProductAccessoryDO> accessoryDOS = longListMap.get(productAccessoryDO.getId());
                accessoryDOS.add(productAccessoryDO);
            } else {
                List<ProductAccessoryDO> accessoryDOS = Lists.newLinkedList();
                accessoryDOS.add(productAccessoryDO);
                longListMap.put(productAccessoryDO.getId(), accessoryDOS);
            }
        }
        for (ProductAccessoryDO productAccessoryDO : productAccessoryDOList) {
            if (!longListMap.containsKey(productAccessoryDO.getId())) {
                insertList.add(productAccessoryDO);
            }
        }
        return insertList;
    }

}
